package com.example.dzchat;

import com.google.gson.Gson;

import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;


public final class JSONUtils {
    public static <T> T parseJSON(String jsonStr, Class<T> t) {
        Gson gson = new Gson();
        T bean = gson.fromJson(jsonStr, t);
        return bean;
    }

    /**
     * @param response
     * @param type     Type type = new TypeToken<ltArrayList<ltAnimeInfo>>() {
     *                 //							 *            }.getType();
     * @return
     */
    public static <T> T parseJSONArray(String response, Type type) {
        Gson gson = new Gson();
        T data = gson.fromJson(response, type);
        return data;
    }

    public static <T> String mapToJson(Map<String, T> map) {
        Gson gson = new Gson();
        String jsonStr = gson.toJson(map);
        return jsonStr;
    }

    public static String ObjectToJson(Object json) {
        Gson gson = new Gson();
        String jsonStr = gson.toJson(json);
        return jsonStr;
    }

    public static HashMap<String, Object> ObjectTomap(String mapJson) {
        Gson gson = new Gson();
        HashMap<String, Object> map = gson.fromJson(mapJson, HashMap.class);
        return map;
    }

}
